//
//  WQCSetPassController.m
//  renxinghuan
//
//  Created by 王晓民 on 2018/4/3.
//  Copyright © 2018年 吴清翠. All rights reserved.
//

#import "WQCSetPassController.h"
#import "configuration.h"
#import "RSA.h"
@interface WQCSetPassController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *setPassTable;
@property(nonatomic,strong)NSArray  *passArr;
//旧密码
@property(nonatomic,strong)UITextField * oldpasstexfield;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;

/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;
@end

@implementation WQCSetPassController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupTableView];
}

- (void)setupNav
{
    self.navigationItem.title = @"修改登录密码";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    
    self.view.backgroundColor = WYCGlobalBg;
}
#pragma make -------->>TableView
- (void)setupTableView{
    self.setPassTable = [[UITableView alloc] initWithFrame:CGRectMake(0, kDevice_Is_iPhoneX?52:28, ScreenWidth,  200) style:UITableViewStyleGrouped];
    self.setPassTable.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.setPassTable.delegate = self;
    self.setPassTable.scrollEnabled = NO;
    self.setPassTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割线
    
    [self.view addSubview:self.setPassTable];
    
    _passArr = @[@"旧密码",@"新密码",@"确认密码"];
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(50, 270,ScreenWidth-100, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 25;
    [loginButton setTitle:@"确定" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loadPass) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}

-(void)loadPass{
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    if(self.oldpasstexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"旧密码不能为空"];
        return;
    }
   
    if(self.passtexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"新密码不能为空"];
        return;
    }
    
    if(self.Zpasstexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"再次输入密码不能为空"];
        return;
    }
    if(self.passtexfield.text != self.Zpasstexfield.text)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次输入密码不一致"];
        return;
    }
NSMutableDictionary * params = [NSMutableDictionary dictionary];
NSString * passStr = [RSA encryptString:_oldpasstexfield.text publicKey:RSA_P_KEY];
NSString *pas = [RSA encryptString:_passtexfield.text publicKey:RSA_P_KEY];
params[@"oldPassword"] = passStr;
params[@"newPassword"] = pas;
    params[@"mobile"] = [WYCAccountTool unarchivemobile];
    params[@"code"] = self.codeStr;


[HttpTool POST:userLpass params:params success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"------77-%@",responseObject);
    
    if ([[responseObject objectForKey:@"code"]intValue]  == 0) {
        [SVProgressHUD showImage:nil status:@"修改登录密码成功"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [SVProgressHUD showImage:nil status:[responseObject objectForKey:@"message"]];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
        
    }
    
} fail:^(NSURLSessionDataTask *task, NSError *error) {
    [SVProgressHUD showImage:nil status:@"网络请求失败"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色

}];
}

#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //分组数 也就是section数
    return 1;
}

//设置每个分组下tableview的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

//设置每行对应的cell（展示的内容）
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = [_passArr objectAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        self.oldpasstexfield = [[UITextField alloc] init];
        self.oldpasstexfield.frame = CGRectMake(150, 10,ScreenWidth-150, 30);
        self.oldpasstexfield.placeholder = @"请输入旧密码";
        self.oldpasstexfield.secureTextEntry = YES;
        [cell.contentView addSubview:self.oldpasstexfield];
    }
    else if (indexPath.row == 1)
    {
        
        self.passtexfield = [[UITextField alloc] init];
        self.passtexfield.frame = CGRectMake(150, 10,ScreenWidth-150, 30);
        self.passtexfield.placeholder = @"请输入密码";
        self.passtexfield.secureTextEntry = YES;
        [cell.contentView addSubview:self.passtexfield];
    }
    else if (indexPath.row == 2)
    {
        self.Zpasstexfield = [[UITextField alloc] init];
        self.Zpasstexfield.frame = CGRectMake(150, 10,ScreenWidth-150, 30);
        self.Zpasstexfield.placeholder = @"再次输入密码";
        self.Zpasstexfield.secureTextEntry = YES;
        [cell.contentView addSubview:self.Zpasstexfield];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
